package com.Maykk.objSerialization;

import java.io.*;

/**
 * @ClassName: Demo01
 * @Description: TODO 通过之前学习的内容来实现对象的存储
 * @Author: MaykkStar@163.com
 */
public class Demo01 {
    public static void main(String[] args) throws IOException {
        // mySave();
        Student stuNew = myLoad();
        System.out.println(stuNew);
    }

    /**
     * 将文件中存储的 Student 对象读取出来
     *
     * @throws IOException
     */
    private static Student myLoad() throws IOException {
        BufferedReader br = new BufferedReader(new FileReader("./Maykk.txt"));
        // System.out.println(Arrays.toString(br.readLine().split("##")));
        String[] st = br.readLine().split("##"); // split 拆分字符串
        br.close();
        return new Student(st[0], st[1], Integer.parseInt(st[2]), Float.parseFloat(st[3]));
    }

    /**
     * 将一个 Student 对象存储到文件里
     *
     * @throws IOException
     */
    private static void mySave() throws IOException {
        Student st = new Student("喝咖啡续命的学生", "男", 18, 100);
        BufferedWriter bw = new BufferedWriter(new FileWriter("Maykk.txt"));
        System.out.println(st);
        bw.write(st.getName() + "##" + st.getSex() + "##" + st.getAge() + "##" + st.getScore());
        bw.flush();

        bw.close();
    }
}